﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
namespace HuynhQuangWebsite.Controllers
{
    public class ManageContactController : BaseController
    {
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        //
        // GET: /ManageContact/

        public ActionResult IndexContact()
        {
            var contact = (from contactus in ctx.ContactUs where contactus.ContactUsID != 0 select contactus).SingleOrDefault();
            if (contact != null)
                return View(contact);
            else
            {
                contact = new Models.ContactU();
                return View(contact);
            }   
        }
        [HttpPost]
        public ActionResult UpdateToContact(int id, FormCollection frm)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOnDashBoard", "DashBoard");

                var contact = (from contactus in ctx.ContactUs where contactus.ContactUsID == id select contactus).SingleOrDefault();
                if (contact != null)
                {
                    contact.ContactAddr = frm["ContactAddr"];
                    contact.ContactAddCity = frm["ContactAddCity"];
                    contact.ContactCountry = frm["ContactCountry"];
                    contact.ContactFax = frm["ContactFax"];
                    contact.ContactPhone1 = frm["ContactPhone1"];
                    contact.ContactMobile = frm["ContactMobile"];

                    contact.ContactTitle = frm["ContactTitle"];
                    contact.ContactDirection = frm["ContactDirection"];
                    contact.ContactWriteToUs = frm["ContactWriteToUs"];
                    contact.ContactGeneral = frm["ContactGeneral"];
                    contact.AACreateDate = DateTime.Now;
                    UpdateModel(contact);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexContact");
                }
                else
                {
                    contact = new ContactU();
                    contact.ContactAddr = frm["ContactAddr"];
                    contact.ContactAddCity = frm["ContactAddCity"];
                    contact.ContactCountry = frm["ContactCountry"];
                    contact.ContactFax = frm["ContactFax"];
                    contact.ContactPhone1 = frm["ContactPhone1"];
                    contact.ContactMobile = frm["ContactMobile"];

                    contact.ContactTitle = frm["ContactTitle"];
                    contact.ContactDirection = frm["ContactDirection"];
                    contact.ContactWriteToUs = frm["ContactWriteToUs"];
                    contact.ContactGeneral = frm["ContactGeneral"];
                    contact.AACreateDate = DateTime.Now;
                    ctx.AddToContactUs(contact);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexContact");
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
